package com.show.controller;

import com.alibaba.fastjson.JSON;
import com.show.pojo.User;
import com.show.service.TraineeService;
import com.show.service.UserService;
import com.show.service.impl.TraineeServiceImpl;
import com.show.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * @author 绛河
 */
@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();

    public void selectByStuNumAndPassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader br = request.getReader();
        String params = br.readLine();
        User user1 = JSON.parseObject(params, User.class);
        System.out.println("数据接收："+user1);
        User user = userService.selectByStuNumAndPassword(user1.getStuNum(), user1.getPassword());
        System.out.println("查询结果："+user);
        if (user!=null) {
            String jsonString = JSON.toJSONString(user);
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
        }
        if (user==null){
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write("登录失败");
        }
    }
}
